//
//  Devices.m
//  EFExtend
//
//  Created by mac on 15/10/4.
//  Copyright © 2015年 Eric. All rights reserved.
//

#import "Devices.h"


@interface Devices ()

@end

@implementation Devices{
    NSMutableArray *boards;
}

- (void)dealloc
{
   
    HM_SUPER_DEALLOC();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];
    
    [self initDatas];
    
    [self initSubviews];
}

- (void)initDatas{
    //    [HMFileCache removeAllImageCaches];
    boards = [[NSMutableArray alloc]init];
    [boards addObject:@{@"title":@"立即定位",@"subtitle":@""}];
    [boards addObject:@{@"title":@"关立即定位",@"subtitle":@""}];
    [boards addObject:@{@"title":@"持续定位",@"subtitle":@""}];
    [boards addObject:@{@"title":@"关持续定位",@"subtitle":@""}];
    [boards addObject:@{@"title":@"定时定位",@"subtitle":@""}];
    [boards addObject:@{@"title":@"关定时定位",@"subtitle":@""}];
    [boards addObject:@{@"title":@"陀螺仪",@"subtitle":@""}];
    [boards addObject:@{@"title":@"关陀螺仪",@"subtitle":@""}];
    [boards addObject:@{@"title":@"模拟定位",@"subtitle":@""}];
    [boards addObject:@{@"title":@"关模拟定位",@"subtitle":@""}];
    [self observeNotification:[HMLocationManager DID_LOCATED]];
    [self observeNotification:[HMLocationManager DID_HEADED]];
}


- (void)initSubviews{
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}

ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {//customNavLeftBtn
            [self backAndRemoveWithAnimate:YES];
        }else if ([btn is:@"rightBtn"]){//customNavRightBtn
            
        }
    }
}


#pragma  mark - table delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return boards.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"listCell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
    }
    NSDictionary *city = [boards objectAtIndex:indexPath.row];
    cell.textLabel.text = [city valueForKey:@"title"];
    cell.detailTextLabel.text = [city valueForKey:@"subtitle"];
    if ([[city valueForKey:@"title"] is:@"持续定位"]) {
        if ([HMLocationManager sharedInstance].requested&LocationRequestType_monitor) {
            cell.detailTextLabel.text = [[HMLocationManager sharedInstance].currentLocation description];
        }
    }else if ([[city valueForKey:@"title"] is:@"立即定位"]) {
        if ([HMLocationManager sharedInstance].requested&LocationRequestType_loc) {
            cell.detailTextLabel.text = [[HMLocationManager sharedInstance].currentLocation description];
        }
    }else if ([[city valueForKey:@"title"] is:@"定时定位"]) {
        if ([HMLocationManager sharedInstance].requested&LocationRequestType_interval) {
            cell.detailTextLabel.text = [[HMLocationManager sharedInstance].currentLocation description];
        }
    }else if ([[city valueForKey:@"title"] is:@"陀螺仪"]) {
        if ([HMLocationManager sharedInstance].requested&LocationRequestType_head) {
            cell.detailTextLabel.text = [[HMLocationManager sharedInstance].currentHeading description];
        }
    }else if ([[city valueForKey:@"title"] is:@"模拟定位"]) {
        cell.detailTextLabel.text = [[HMLocationManager sharedInstance].currentLocation description];
    }
    cell.detailTextLabel.numberOfLines = 0;
    cell.detailTextLabel.textColor = RGB(.5, .5, .5);
    return cell;
}

- (void)takeController:(HMUICamera *)controller gotPhoto:(UIImage *)photo withInfo:(NSDictionary *)info{
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *city = [boards objectAtIndex:indexPath.row];
//    NSString *map = [city valueForKey:@"map"];
    NSString *title = [city valueForKey:@"title"];

    if ([title is:@"立即定位"]) {
        [HMLocationManager sharedInstance].requested |= LocationRequestType_loc;
    }else if ([title is:@"关立即定位"]) {
        [HMLocationManager sharedInstance].requested &= ~LocationRequestType_loc;
    }else if ([title is:@"持续定位"]) {
        [HMLocationManager sharedInstance].requested |= LocationRequestType_monitor;
    }else if ([title is:@"关持续定位"]) {
        [HMLocationManager sharedInstance].requested &= ~LocationRequestType_monitor;
    }else if ([title is:@"定时定位"]) {
        [HMLocationManager sharedInstance].distanceFilter = kCLDistanceFilterNone;
        [HMLocationManager sharedInstance].desiredAccuracy = kCLLocationAccuracyBest;
        [HMLocationManager sharedInstance].timerSpace = 10;
        [HMLocationManager sharedInstance].requested |= LocationRequestType_interval;
    }else if ([title is:@"关定时定位"]) {
        [HMLocationManager sharedInstance].requested &= ~LocationRequestType_interval;
    }else if ([title is:@"重力传感器"]) {
        [HMLocationManager sharedInstance].requested |= LocationRequestType_head;
    }else if ([title is:@"关重力传感器"]) {
        [HMLocationManager sharedInstance].requested &= ~LocationRequestType_head;
    }else if ([title is:@"模拟定位"]) {
        NSMutableArray *locations = [NSMutableArray array];
        [locations addObject:[[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(26.05, 119.11) altitude:10 horizontalAccuracy:20 verticalAccuracy:10 course:26 speed:30 timestamp:[NSDate date]]];
        [locations addObject:[[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(26.06, 119.11) altitude:10 horizontalAccuracy:20 verticalAccuracy:10 course:26 speed:30 timestamp:[NSDate date]]];
        [locations addObject:[[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(26.06, 119.11) altitude:10 horizontalAccuracy:20 verticalAccuracy:10 course:26 speed:30 timestamp:[[NSDate date] dateByAddingTimeInterval:30]]];
        
        [[HMLocationManager sharedInstance] simulationLocations:locations];
    }else if ([title is:@"关模拟定位"]) {
        [[HMLocationManager sharedInstance] dissimulationLocations];
    }
    
}

ON_NOTIFI_Location(DID_HEADED, _nofi){
    
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
    
}

ON_NOTIFI_Location(DID_LOCATED, _nofi){
    
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
    
    if ([HMLocationManager sharedInstance].requested & LocationRequestType_loc) {
        //        [HMLocationManager sharedInstance].requested &= ~LocationRequestType_loc;
    }
}

@end
